package com.tio.showcase.common;

import org.tio.core.ChannelContext;
import org.tio.core.GroupContext;
import org.tio.core.exception.AioDecodeException;
import org.tio.core.intf.AioHandler;
import org.tio.core.intf.Packet;

import java.nio.ByteBuffer;

/**
 * Author      : liyuemin
 * Time        : 2017/12/15 11:47
 * Description :
 */
public abstract class ShowcaseAsAioHandler implements AioHandler {

    /**
    * @Description : 解码：把接受到的 bytebuffer ，解码成应用可以识别的业务消息包
     * 消息头：type+ bodylength;
     * 消息体：byte[]
    * @param       : [byteBuffer, channelContext]
    * @return      : org.tio.core.intf.Packet
    * @author      : liyuemin
    * @date        : 2017/12/15
    */
    public Packet decode(ByteBuffer byteBuffer, ChannelContext channelContext) throws AioDecodeException {
        int readableLength = byteBuffer.limit()- byteBuffer.position();
        if(readableLength<ShowcasePacket.HEADER_LENGHT){
            return null;
        }
        //消息类型
        byte type = byteBuffer.get();
        System.out.println("1position位置:"+byteBuffer.position());
        int bodyLength  = byteBuffer.getInt();
        System.out.println("2position位置:"+byteBuffer.position());
        if(bodyLength<0){
            throw new AioDecodeException("bodyLength [" + bodyLength + "] is not right, remote:" + channelContext.getClientNode());
        }
        int needLength = ShowcasePacket.HEADER_LENGHT+bodyLength;
        int isgou = readableLength - needLength;//读取的长度- 需要的长度
        if(isgou<0){
            return null;
        }else{
            ShowcasePacket imPacket = new ShowcasePacket();
            imPacket.setType(type);
            if(bodyLength>0){
                byte[] dts = new byte[bodyLength];
                byteBuffer.get(dts);
                imPacket.setBody(dts);
            }
            return imPacket;
        }
    }

    /**
    * @Description : 编码： 将业务消息包编码为可发送ByteBuffer
     * 消息头：type+bodylength
     * 消息体: byte[]
    * @param       : [packet, groupContext, channelContext]
    * @return      : java.nio.ByteBuffer
    * @author      : liyuemin
    * @date        : 2017/12/15
    */
    public ByteBuffer encode(Packet packet, GroupContext groupContext, ChannelContext channelContext) {
        ShowcasePacket showcasePacket = (ShowcasePacket) packet;

        byte[] body = showcasePacket.getBody();
        int bodylength = 0;
        if(body!= null){
            bodylength = body.length;
        }
        int allLength = ShowcasePacket.HEADER_LENGHT+bodylength;
        ByteBuffer buffer = ByteBuffer.allocate(allLength);
        buffer.order(groupContext.getByteOrder());
        buffer.put(showcasePacket.getType());
        System.out.println("3position位置:"+buffer.position());
        buffer.putInt(bodylength);//写入消息体长度
        System.out.println("4position位置:"+buffer.position());
        if(body!= null){
            buffer.put(body);
            System.out.println("5position位置:"+buffer.position());
        }
        return buffer;
    }


}
